package cn.net.autocode.platform.assistant;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.cache.AutoCacheUtil;
import cn.net.autocode.platform.design.handle.AutoCodeHandle;
import cn.net.autocode.platform.design.vo.AppInfo;
import cn.net.autocode.platform.design.vo.DeveloperInfo;
import cn.net.autocode.platform.design.webclient.AcWebClient;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("platformAssistants")
@RequestMapping("autocode/assistants")
public class PlatformAssistants {
    private final AutoCacheUtil autoCacheUtil;
    private final AcWebClient acWebClient;
    private final Dao dao;


    public PlatformAssistants(AutoCacheUtil autoCacheUtil, AcWebClient acWebClient, Dao dao) {
        this.acWebClient = acWebClient;
        this.autoCacheUtil = autoCacheUtil;
        this.dao = dao;
    }

    @ResponseBody
    @RequestMapping("chat")
    public ResultT chat(@RequestParam(value = "message") String message,
                         @RequestParam(value = "role") String role,
                         @RequestParam(value = "chatId",required = false) String chatId){

        /*
          项目信息
         */
        AppInfo appInfo = AutoCodeHandle.getAppInfo();
        if (appInfo==null){
            return ResultT.fail("没有设置授权信息，请登陆(https://autocode.net.cn)下载license.lic授权文件，下载后复制到resources目录下，重启应用！");
        }

        /*
          获取开发者账号
         */
        DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
        if(developerInfo==null){
           return ResultT.fail("没有登陆开发者账号，请先在右上角的【开发者信息】栏中进行登陆！");
        }
        if (role==null || role.isEmpty()){
            return ResultT.fail("请选择AI角色！");
        }

        if (message==null || message.isEmpty()){
            return ResultT.fail("发送信息不能为空！");
        }

        String dbType = dao.getDbType();

        JSONObject param = new JSONObject();
        param.put("chatId", chatId);
        param.put("role", role);
        param.put("message", message);
        param.put("dbType", dbType);

        JSONObject rs = acWebClient.assistantChat(param);

        return ResultT.success(rs);
    }



    @ResponseBody
    @RequestMapping("clearChat")
    public ResultT clearChat(@RequestBody JSONObject params){
        String chatId = params.getString("chatId");
        /*
          项目信息
         */
        AppInfo appInfo = AutoCodeHandle.getAppInfo();
        if (appInfo==null){
            return ResultT.fail("没有设置授权信息，请登陆(https://autocode.net.cn)下载license.lic授权文件，下载后复制到resources目录下，重启应用！");
        }

        /*
          获取开发者账号
         */
        DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
        if(developerInfo==null){
            return ResultT.fail("没有登陆开发者账号，请先在右上角的【开发者信息】栏中进行登陆！");
        }

        String userName = developerInfo.getUsername();
        autoCacheUtil.del("large_model_"+userName+"_"+chatId);
        return ResultT.success();
    }

    @ResponseBody
    @RequestMapping("getLastChat")
    public ResultT getLastChat(@RequestBody JSONObject params){
        /*
          项目信息
         */
        AppInfo appInfo = AutoCodeHandle.getAppInfo();
        if (appInfo==null){
            return ResultT.fail("没有设置授权信息，请登陆(https://autocode.net.cn)下载license.lic授权文件，下载后复制到resources目录下，重启应用！");
        }

        /*
          获取开发者账号
         */
        DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
        if(developerInfo==null){
            return ResultT.fail("没有登陆开发者账号，请先在右上角的【开发者信息】栏中进行登陆！");
        }

        JSONObject rs = acWebClient.assistantLastChat(params);
        return ResultT.success(rs);
    }




}
